package com.bluezone.dec.system.service.impl;

import com.bluezone.dec.common.CacheConstant;
import com.bluezone.dec.common.PageEntity;
import com.bluezone.dec.system.dao.EntRectRecordMapper;
import com.bluezone.dec.system.entity.EntRectRecord;
import com.bluezone.dec.system.service.EntRectRecordService;
import com.bluezone.dec.system.vo.PlanRecordVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author HHL
 * @description 企业专项整治记录service实现类
 * @date 2018/10/8 - 15:02
 */
@Service
public class EntRectRecordServiceImpl implements EntRectRecordService {

    private static Logger log = LoggerFactory.getLogger(EntRectRecordServiceImpl.class);

    @Autowired
    private EntRectRecordMapper entRectRecordMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 根据专项整治计划id删除关联的专项整治记录
     *
     * @param planId
     * @return
     */
    public int deleteRecordByPlanId(String planId) {
        return entRectRecordMapper.deleteRecordByPlanId(planId);
    }

    /**
     * 根据传递的map参数，动态分页查询专项整治记录数据
     *
     * @param params
     * @return
     */
    @Override
    public PageEntity<EntRectRecord> findByParams(Map<String, Object> params) {

        int pageNum = (int) params.get("pageNum");// 页码
        int pageSize = (int) params.get("pageSize"); // 页大小

        List<EntRectRecord> list = null;
        PageInfo<EntRectRecord> pageInfo = null;

        if (pageNum != 0 && pageSize != 0) {
            PageHelper.startPage(pageNum, pageSize); // PageHelper分页插件分类
            list = entRectRecordMapper.findByParams(params);
            pageInfo = new PageInfo(list);
        }

        return PageEntity.toPageEntity(pageInfo);
    }

    /**
     * 根据记录id删除企业专项整治记录
     *
     * @param ids
     * @return
     */
    public int deleteRecordByRecordId(List<String> ids) {

        int i = 0;
        int j = 0;

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.TASK_CENSUS))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.TASK_CENSUS);
        }

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.INVEST_STATISTIC))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.INVEST_STATISTIC);
        }

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.UNFINISH_RECTIFICATION_COUNT))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.UNFINISH_RECTIFICATION_COUNT);
        }

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.SCHEDULE_TASKS_IMPLEMENT))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.SCHEDULE_TASKS_IMPLEMENT);
        }

        if (!StringUtils.isEmpty(ids)) {
            try {
                for (j = 0; j < ids.size(); j++) {
                    i = entRectRecordMapper.deleteByPrimaryKey(ids.get(j));
                }
            } catch (Exception e) {
                log.warn("根据记录id删除企业专项整治记录失败", e);
            }
        }

        return i;
    }

    /**
     * 根据记录id删除专项整治记录
     *
     * @param id
     * @return
     */
    public int remove(String id) {
        return 0;
    }

    /**
     * 根据记录id更新企业专项整治记录
     *
     * @param entRectRecord
     * @return
     */
    public int update(EntRectRecord entRectRecord) {

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.TASK_CENSUS))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.TASK_CENSUS);
        }

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.INVEST_STATISTIC))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.INVEST_STATISTIC);
        }

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.UNFINISH_RECTIFICATION_COUNT))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.UNFINISH_RECTIFICATION_COUNT);
        }

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.SCHEDULE_TASKS_IMPLEMENT))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.SCHEDULE_TASKS_IMPLEMENT);
        }

        return entRectRecordMapper.updateByPrimaryKeySelective(entRectRecord);
    }

    /**
     * 通过企业编号查询专项整治记录表里某企业对应的专项整治计划
     *
     * @param entCode 企业编号
     * @return
     */
    @Override
    public List<PlanRecordVo> findAllPlanByEnterpriseCode(String entCode) {
        return entRectRecordMapper.findAllPlanByEnterpriseCode(entCode);
    }

    /**
     * 查询企业专项整治记录表里存在的所有企业
     *
     * @return
     */
    @Override
    public List<EntRectRecord> findAllEnterprise() {
        return entRectRecordMapper.findAllEnterprise();
    }

    /**
     * 添加企业专项整治记录
     *
     * @param entRectRecord
     * @return
     */
    @Override
    public int save(EntRectRecord entRectRecord) {

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.TASK_CENSUS))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.TASK_CENSUS);
        }

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.INVEST_STATISTIC))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.INVEST_STATISTIC);
        }

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.UNFINISH_RECTIFICATION_COUNT))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.UNFINISH_RECTIFICATION_COUNT);
        }

        if (!Strings.isNullOrEmpty(redisTemplate.opsForValue().get(CacheConstant.SCHEDULE_TASKS_IMPLEMENT))) {
            redisTemplate.opsForValue().getOperations().delete(CacheConstant.SCHEDULE_TASKS_IMPLEMENT);
        }

        return entRectRecordMapper.insertSelective(entRectRecord);
    }

    @Override
    public EntRectRecord get(String id) {
        return null;
    }

    @Override
    public int batchRemove(String... ids) {
        return 0;
    }

    @Override
    public List<EntRectRecord> findAll() {
        return new ArrayList<>();
    }

}
